/**
 * 
 */
package com.netflexitysolutions.amazonws.sdb.orm.metadata;


/**
 * @author netflexity
 *
 * Contains all the objects and attributes that belong to 
 * &lt;many-to-one&gt; or &lt;bag&gt; with one-to-many XML element.
 */
public class ManyToOneMetadata extends AttributeMetadata{

	private String fetchType;

	/**
	 * 
	 */
	public ManyToOneMetadata() {
		setLazy("true");
	}
	
	/**
	 * @return the fetchType
	 */
	public String getFetchType() {
		return fetchType;
	}

	/**
	 * @param fetchType the fetchType to set
	 */
	public void setFetchType(String fetchType) {
		this.fetchType = fetchType;
	}

	/* (non-Javadoc)
	 * @see com.netflexitysolutions.amazonws.sdb.orm.metadata.AttributeMetadata#toSimpleDB(java.lang.Object)
	 */
	@Override
	public <T> String toSimpleDB(T record) {
		// Get metadata for manyToOne bean itself.
		ItemMetadata manyToOneItemMetadata = getItemMetadata().getMapping().getItemMetadataByClass(getJavaClass());
		assert(manyToOneItemMetadata != null);
		
		//Get primary key info.
		IdMetadata primaryKeyMetadata = manyToOneItemMetadata.getId();
		assert(primaryKeyMetadata != null);
		
		// Get manyToOne object itself.
		Object manyToOneBean = getFieldValue(record);
		
		// Get value.
		if(manyToOneBean != null){
			return primaryKeyMetadata.toSimpleDB(manyToOneBean);
		}
		return null;
	}
}
